package com.zking.myservlet.product;

import com.zking.dao.CategoryDao;
import com.zking.dao.ProductDao;
import com.zking.entity.Category;
import com.zking.entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/manage/ProductModify")
public class ProductModify extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");

        int id = Integer.parseInt(req.getParameter("id"));
        int childId = Integer.parseInt(req.getParameter("childId"));
        String name = req.getParameter("name");
        String description = req.getParameter("description");
        Product tmp = ProductDao.selectProductById(id);
        req.setAttribute("name", name);
        req.setAttribute("description", description);
        List<Category> list = CategoryDao.getAllCategory();
        req.setAttribute("bigList", list);

        if(tmp!=null){
            req.setAttribute("count", tmp.getCount());
            req.setAttribute("price", tmp.getEp_price());

        }
        req.setAttribute("id", id);
        req.setAttribute("defaultClassId", childId);

        req.getRequestDispatcher("product-modify.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
